﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Yipusoft.Account.BLL;

public partial class KeyUnit_ListView : System.Web.UI.Page
{
    private static int totalPage = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Init();
            if (Request["Flag"] == null)
            {
                dptDepartment.SelectedValue = "0000";
                // MonthMenu.Items[0].Selected = true;
            }
            else
            {
                dptDepartment.SelectedValue = Request["DepartmentID"].ToString().Trim();
            }
            NewsListBind("1=1", 1);
        }

    }
    private void Init()
    {
        if (string.IsNullOrEmpty(Request["DepartmentID"]))
            return;

        BindDepartment();
        BindKeyUnitType();
        foreach (MenuItem item in this.MonthMenu.Items)
        {
            if (item.Value.Equals(Request["TypeID"].ToString().Trim()))
            {
                item.Selected = true;
                SetSeletedMenuBG();
            }

        }

        // NewsListBind("1=1", 1);
    }

    private void BindDepartment()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand("Select * Into  ##myDepartments4 From Account_Departments  "
+ " Insert into ##myDepartments4 Select (Left([DepartmentID],4)+'5'+right([DepartmentID],2))as 'DepartmentID' ,[FullName] ,REPLACE(REPLACE(REPLACE([ShortName],'消防大队','派出所'),'消防科','派出所'),'大队','派出所')as 'ShortName' ,[AliasName] ,[ParentID] ,[State] From Account_Departments Where Left(DepartmentID,4)<>'1300'  And SubString(DepartmentID,5,1)='2'"
 + " Select * From ##myDepartments4 Where Left(DepartmentID,4)=left(" + Request["DepartmentID"].ToString() + ",4) And (SubString(DepartmentID,5,1)=2 or SubString(DepartmentID,5,1)=5) drop table ##myDepartments4", con);
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        DataTable dtb = new DataTable();
        dap.Fill(dtb);
        DataRow drw2 = dtb.NewRow();
        drw2["DepartmentID"] = Request["DepartmentID"].ToString().Substring(0, 4) + "000";
        drw2["ShortName"] = Department.GetDepartment(Request["DepartmentID"].ToString().Substring(0, 4) + "000").ShortName + "防火处";
        dtb.Rows.InsertAt(drw2, 0);
        DataRow drw = dtb.NewRow();
        drw["DepartmentID"] = "0000";
        drw["ShortName"] = "全部单位";
        dtb.Rows.InsertAt(drw, 0);
        this.dptDepartment.DataSource = dtb;
        this.dptDepartment.DataTextField = "ShortName";
        this.dptDepartment.DataValueField = "DepartmentID";
        this.dptDepartment.DataBind();

    }
    private void BindKeyUnitType()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand("Select * From KeyUnitType Order by cast(KeyUnitTypeID as int)", con);
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        DataTable dtb = new DataTable();
        dap.Fill(dtb);
        foreach (DataRow rw in dtb.Rows)
        {
            MenuItem mitem = new MenuItem(rw["KeyUnitTypeDescript"].ToString(), rw["KeyUnitTypeID"].ToString());
            // mitem.ToolTip = rw["KeyUnitTypeName"].ToString();
            this.MonthMenu.Items.Add(mitem);
        }
        MonthMenu.Items.AddAt(0, new MenuItem("已更新", "13"));
        MonthMenu.Items.AddAt(0, new MenuItem("全部", "0000"));
    }
    protected void MonthMenu_MenuItemClick(object sender, MenuEventArgs e)
    {
        NewsListBind("1=1", 1);
        SetSeletedMenuBG();
    }
    private void SetSeletedMenuBG()
    {
        foreach (MenuItem item in MonthMenu.Items)
        {
            if (item.Selected)
            {
                item.ImageUrl = "../Images/select_Item.gif";
            }
            else
            {
                item.ImageUrl = "../Images/no_select_Item.gif";
            }
        }
        lblTitle.Text = dptDepartment.SelectedItem.Text + "重点单位列表";

    }
    private void BindGridView()
    {
        lblWhere.Text = " 1=1";
        string type = string.Empty;
        foreach (MenuItem item in MonthMenu.Items)
        {
            if (item.Selected && (!item.Value.Equals("0000")))
                type = item.Value;
        }

        if (dptDepartment.SelectedValue.Equals("0000"))
        {
            lblWhere.Text += " And Left(a.DepartmentID,4)=" + Request["DepartmentID"].ToString().Substring(0, 4);
        }
        else if (dptDepartment.SelectedValue == (Request["DepartmentID"].Substring(0, 4) + "000"))
        {
            lblWhere.Text += " And Left(a.DepartmentID,4)=" + Request["DepartmentID"].Substring(0, 4) + " And (SubString(a.DepartmentID,5,1)=0 or SubString(a.DepartmentID,5,1)=1) ";
        }
        else
        {
            lblWhere.Text += " And a.DepartmentID=" + dptDepartment.SelectedValue;
        }
        if (type != string.Empty)
        {
            if (type != "13")
                lblWhere.Text += " And a.KeyUnitTypeID=" + type;
            else
            {
                DateTime dat = new DateTime();//2010, 9, 15);
                dat = Convert.ToDateTime(ConfigurationManager.AppSettings["keyunituptime"].ToString());
                lblWhere.Text += " And a.UpTime>='" + dat.ToString() + "'";
                //lblWhere.Text += " And a.UpTime>='2010-9-7 0:00:00'";
            }
        }
        lblWhere.Text += " And a.PerformanceState ='已完成'";
        //SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        //SqlCommand cmd = new SqlCommand(strSql, con);
        //cmd.CommandType = CommandType.Text;
        //SqlDataAdapter dap = new SqlDataAdapter(cmd);
        //DataTable dtb = new DataTable();
        //dap.Fill(dtb);
        //this.gridList.DataSource = dtb;
        //this.gridList.DataBind();
    }
    protected void dptDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        NewsListBind("1=1", 1);
        lblTitle.Text = dptDepartment.SelectedItem.Text + "重点单位列表";
    }
    protected void NavHome_Click(object sender, EventArgs e)
    {
        NewsListBind(lblWhere.Text, 1);
    }
    protected void NavPre_Click(object sender, EventArgs e)
    {
        NewsListBind(lblWhere.Text, Convert.ToInt32(CurrPage.Text) - 1);
    }
    protected void NavNext_Click(object sender, EventArgs e)
    {
        NewsListBind(lblWhere.Text, Convert.ToInt32(CurrPage.Text) + 1);
    }
    protected void NavLast_Click(object sender, EventArgs e)
    {
        NewsListBind(lblWhere.Text, Convert.ToInt32(PagesCount.Text));
    }

    protected void HotNews1_Load(object sender, EventArgs e)
    {

    }
    private void NewsListBind(string where, int currPage)
    {
        BindGridView();
        int pageSize = 20;
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand("Get4Power_Page", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Where", SqlDbType.NVarChar).Value = lblWhere.Text;
        cmd.Parameters.Add("@CurrPage", SqlDbType.Int).Value = currPage;
        cmd.Parameters.Add("@PageSize", SqlDbType.Int).Value = pageSize;
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        DataSet dst = new DataSet();
        dap.Fill(dst);

        DataTable myDT = dst.Tables[0];
        this.gridList.DataSource = myDT;
        this.gridList.DataBind();

       


        CurrPage.Text = currPage.ToString();
        TotalNum.Text = dst.Tables[1].Rows[0][0].ToString();
        PagesCount.Text = ((int.Parse(dst.Tables[1].Rows[0][0].ToString()) % pageSize) > 0 ? (int.Parse(dst.Tables[1].Rows[0][0].ToString()) / pageSize) + 1 : int.Parse(dst.Tables[1].Rows[0][0].ToString()) / pageSize).ToString();
        totalPage = Convert.ToInt32(PagesCount.Text);
        if (currPage == 1)
        {
            NavHome.Enabled = false;
            NavPre.Enabled = false;
        }
        else
        {
            NavHome.Enabled = true;
            NavPre.Enabled = true;
        }
        if ((currPage == totalPage) || (totalPage == 0))
        {
            NavNext.Enabled = false;
            NavLast.Enabled = false;
        }
        else
        {
            NavNext.Enabled = true;
            NavLast.Enabled = true;
        }

    }
    protected void gridList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int i;
        int count = this.gridList.Rows.Count;
        for (i = 0; i <= count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                

                try
                {
                  
                    DateTime dtb = new DateTime();//2010, 9, 15);
                    dtb = Convert.ToDateTime(ConfigurationManager.AppSettings["keyunituptime"].ToString());

                   
                    //更新的时间大于2010-3-5的

                    DateTime dt = Convert.ToDateTime(e.Row.Cells[1].Text);

                    if (dt > dtb)
                    {
                        e.Row.Cells[0].ForeColor = System.Drawing.Color.Blue;
                    }

                    //添加的时间大于2010-3-5的

                    //DateTime dtadd = Convert.ToDateTime(gridList.DataKeys[i]["WriteTime"].ToString());
                    DateTime dtadd = Convert.ToDateTime(e.Row.Cells[3].Text);
                    if (dtadd > dtb)
                    {
                        e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
                        //
                        //gridList.Rows[i].Cells[0].ForeColor = System.Drawing.Color.Red;
                        //e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
                        //e.Row.Cells[0].Attributes.Add("ForeColor", "red");
                        //e.Row.Cells[0].CssClass = "style6";
                        //e.Row.Cells[0].Attributes.Add("class", "style6");
                        //gridList.Rows[i].Cells[0].CssClass = "style6";
                        //Label hl = (Label)gridList.Rows[i].Cells[0];
                        //hl.ForeColor = System.Drawing.Color.Red;
                        //gridList.Rows[i].Cells[0] = "★" + gridList.Rows[i].Cells[0];
                    }
                }
                catch
                {
                }
            }
        }
    }
}
